﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MirrorDriver;
using Providers.LiveControl.Server;
using Rectangle = System.Drawing.Rectangle;

namespace TestScreen
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Load += Form1_Load;
            MirrorDriver = new DesktopMirror();

        }

        public void ShotByMirror()
        {
            if (MirrorDriver.State != DesktopMirror.MirrorState.Running)
            {
                // Most likely first time
                // Start the mirror driver
                MirrorDriver.Load();

                MirrorDriver.Connect();
                MirrorDriver.Start();
            }
            else if (MirrorDriver.State == DesktopMirror.MirrorState.Running)
            {
                Bitmap bm = MirrorDriver.GetScreen();
                //bm.Dispose();

                string pre = _screenShotHelper.FlagShotState ? "" : "截不到";
                
                 bm.Save($@"D:\temp\{pre}{DateTime.Now.ToFileTime()}.png");

                
            }
        }
        public DesktopMirror MirrorDriver { get; set; }

        private void Form1_Load(object sender, EventArgs e)
        {

            StartApploop();
        }
      
        readonly ScreenShotHelper _screenShotHelper = new ScreenShotHelper();
        protected virtual void Heartbeat()
        {
            //foreach (int myCode in Enum.GetValues(typeof(CopyPixelOperation)))
            //{
            //    for (int i = 0; i < 50; i++)
            //    {
            //        string strName = Enum.GetName(typeof(CopyPixelOperation), myCode);//获取名称
            //        string strVaule = myCode.ToString();//获取值

            //        _screenShotHelper.ScreenShot((CopyPixelOperation)myCode);
            //        gdiScreen1.Draw(_screenShotHelper[0], Screen.PrimaryScreen.Bounds);
            //    }

            //}
            _screenShotHelper.ScreenShot();


            gdiScreen1.Draw(_screenShotHelper[0], Screen.PrimaryScreen.Bounds);

            if (DateTime.Now > shotTime)
            {
                ShotByMirror();
                shotTime = DateTime.Now.AddSeconds(2);
            }

            //gdiScreen1.Draw(_screenShotHelper.GetScreenshot(), Screen.PrimaryScreen.Bounds);
        }

        private Bitmap GetScreenshot()
        {
            Size sz = Screen.PrimaryScreen.Bounds.Size;
            IntPtr hDesk = GetDesktopWindow();
            IntPtr hSrce = GetWindowDC(hDesk);
            IntPtr hDest = CreateCompatibleDC(hSrce);
            IntPtr hBmp = CreateCompatibleBitmap(hSrce, sz.Width, sz.Height);
            IntPtr hOldBmp = SelectObject(hDest, hBmp);
            bool b = BitBlt(hDest, 0, 0, sz.Width, sz.Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
            Bitmap bmp = Bitmap.FromHbitmap(hBmp);
            SelectObject(hDest, hOldBmp);
            DeleteObject(hBmp);
            DeleteDC(hDest);
            ReleaseDC(hDesk, hSrce);
            return bmp;
            //bmp.Save(@"c:\temp\test.png");
            //bmp.Dispose();
        }

        // P/Invoke declarations
        [DllImport("gdi32.dll")]
        static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int
        wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop);
        [DllImport("user32.dll")]
        static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);
        [DllImport("gdi32.dll")]
        static extern IntPtr DeleteDC(IntPtr hDc);
        [DllImport("gdi32.dll")]
        static extern IntPtr DeleteObject(IntPtr hDc);
        [DllImport("gdi32.dll")]
        static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
        [DllImport("gdi32.dll")]
        static extern IntPtr CreateCompatibleDC(IntPtr hdc);
        [DllImport("gdi32.dll")]
        static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
        [DllImport("user32.dll")]
        public static extern IntPtr GetDesktopWindow();
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowDC(IntPtr ptr);
        private Thread _mLoopThread;

        private DateTime shotTime = DateTime.Now;
        protected void StartApploop()
        {
         
            _mLoopThread = new Thread(Loop)
            {
                Name = "",
                IsBackground = true
            };
            _mLoopThread.Start();

        }

        private bool _flagRun;


        private void Loop()
        {
            _flagRun = true;
            do
            {
                try
                {
                    Heartbeat();
                }
                catch (Exception)
                {
                    // ignored
                }
            } while (_flagRun );
        }

        public void StopLoop()
        {
            _flagRun = false;
        }
    }
}
